package com.example.hu.UI;

import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.csht.facelib.callback.CameraDataCallback;
import com.csht.facelib.camera.AutoTexturePreviewView;
import com.csht.facelib.camera.CameraPreviewManager;
import com.csht.facelib.utils.Constants;
import com.csht.facelib.utils.NdkLoader;
import com.csht.facelib.utils.SpUtils;
import com.csht.facelib.utils.ToastUtils;
import com.example.hu.R;
import com.example.hu.Utils.ImageUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CameraActivity extends BaseActivity{

    private byte[] data;
    private int width,height;
    private ExecutorService es = Executors.newSingleThreadExecutor();
    private Future future;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        initCamera();
        findViewById(R.id.btn_take).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takePhoto();
            }
        });
    }

    private void takePhoto() {
        if (data != null){
            if (future != null && !future.isDone()) {
                ToastUtils.showToast("正在保存图片，请稍后再试");
                return;
            }
            future = es.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        byte[] yuvData;
                        if (SpUtils.getDetectDirection() == 90) {
                            if (SpUtils.isDataMirror())
                                yuvData = NdkLoader.rotateYUVDegree90AndMirror(data,width ,height);
                            else
                                yuvData = NdkLoader.rotateYUVDegree90(data,width ,height);
                        } else if (SpUtils.getDetectDirection() == 270) {
                            if (SpUtils.isDataMirror())
                                yuvData = NdkLoader.rotateYUVDegree270AndMirror(data,width ,height);
                            else
                                yuvData = NdkLoader.rotateYUVDegree270(data,width ,height);
                        } else {
                            if (SpUtils.isDataMirror())
                                yuvData = NdkLoader.mirror(data,width,height);
                            else
                                yuvData = data;
                        }
                        String filePath = ImageUtil.takePhoto(Constants.ID_PHOTO_PATH, yuvData,
                                SpUtils.getDetectDirection() == 90 || SpUtils.getDetectDirection() == 270?height:width ,
                                SpUtils.getDetectDirection() == 90 || SpUtils.getDetectDirection() == 270?width:height );
                        Intent intent = new Intent();
                        intent.putExtra("filePath",filePath);
                        setResult(RESULT_OK,intent);
                        finish();
                    } catch (Exception e){
                        Log.e("zxbs",e.getMessage());
                        e.printStackTrace();
                    }
                }
            });
        } else {
            ToastUtils.showToast("数据错误");
        }
    }

    private void initCamera() {
        CameraPreviewManager.getInstance().setCameraFacing(CameraPreviewManager.CAMERA_FACING_FRONT);
        CameraPreviewManager.getInstance().startPreview((AutoTexturePreviewView)findViewById(R.id.auto_camera_preview_view), SpUtils.getCameraWidth(), SpUtils.getCameraHeight(), new CameraDataCallback() {
            @Override
            public void onGetCameraData(byte[] data, Camera camera, int width, int height) {
                CameraActivity.this.data = data;
                CameraActivity.this.width = width;
                CameraActivity.this.height = height;
            }

            @Override
            public void onCameraParams(int width, int height) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CameraPreviewManager.getInstance().stopPreview();
    }
}
